// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/python/init/PyInit.h>
#include <pollux/common/memory/memory.h>
#include <pollux/connectors/hive/hive_connector_split.h>
#include <pollux/connectors/hive/hive_data_sink.h>
#include <pollux/connectors/hive/table_handle.h>
#include <pollux/connectors/tpch/tpch_connector_split.h>
#include <pollux/core/plan_node.h>
#include <pollux/dwio/dwrf/register_dwrf_reader.h>
#include <pollux/dwio/dwrf/register_dwrf_writer.h>
#include <pollux/dwio/dwrf/writer/writer.h>
#include <pollux/functions/prestosql/aggregates/register_aggregate_functions.h>
#include <pollux/functions/prestosql/registration/registration_functions.h>
#include <pollux/parse/type_resolver.h>

namespace kumo::pollux::py {

melon::once_flag registerOnceFlag;

void registerAllResourcesOnce() {
  pollux::filesystems::registerLocalFileSystem();

  // Register file readers and writers.
  pollux::dwrf::registerDwrfWriterFactory();
  pollux::dwrf::registerDwrfReaderFactory();

  pollux::dwio::common::LocalFileSink::registerFactory();

  pollux::parse::registerTypeResolver();

  pollux::core::PlanNode::registerSerDe();
  pollux::Type::registerSerDe();
  pollux::common::Filter::registerSerDe();
  pollux::connector::hive::LocationHandle::registerSerDe();
  pollux::connector::hive::HiveSortingColumn::registerSerDe();
  pollux::connector::hive::HiveBucketProperty::registerSerDe();
  pollux::connector::hive::HiveTableHandle::registerSerDe();
  pollux::connector::hive::HiveColumnHandle::registerSerDe();
  pollux::connector::hive::HiveInsertTableHandle::registerSerDe();
  pollux::core::ITypedExpr::registerSerDe();

  // Register functions.
  // TODO: We should move this to a separate module so that clients could
  // register only when needed.
  pollux::functions::prestosql::registerAllScalarFunctions();
  pollux::aggregate::prestosql::registerAllAggregateFunctions();
}

void registerAllResources() {
  melon::call_once(registerOnceFlag, registerAllResourcesOnce);
}

void initializePolluxMemory() {
  if (not pollux::memory::MemoryManager::testInstance()) {
    // Enable full Pollux stack trace when exceptions are thrown.
    FLAGS_pollux_exception_user_stacktrace_enabled = true;
    pollux::memory::initializeMemoryManager({});
  }
}

} // namespace kumo::pollux::py
